package com.doctor.windflower_doctor.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.activity.MySlipSwitch.OnSwitchListener;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.delegate.WindflowerDelegate;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.pop.SureCanclePop;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.view.RoundedImageView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.tencent.android.tpush.XGPushManager;

/**
 * @author Administrator
 * 
 */
public class SetUserActivity extends BaseActivity {
	private RelativeLayout tv_password;
	private TextView tv_phone, tv_email;
	private TextView tv_title;
	private ImageView iv_backs;
	private RelativeLayout tv_sure;
	private String imgurl;
	private boolean stopService;
	private RoundedImageView iv_user;
	private DisplayImageOptions options;
	private TextView tv_finish;
	private MySlipSwitch slipswitch_MSL;
	private String name;
	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		tv_password.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				startActivity(new Intent(SetUserActivity.this, PasswordActivity.class));
			}
		});
		iv_backs.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		tv_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// startActivity(new
				// Intent(SetUserActivity.this,AttestationActivity.class));
				Toast.makeText(getApplicationContext(), "该功能暂未开放", Toast.LENGTH_SHORT).show();
			}
		});
		tv_finish.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				SureCanclePop sureCanclePop = new SureCanclePop(SetUserActivity.this, "确定退出吗？");
				sureCanclePop.setWindflowerDelegate(new WindflowerDelegate() {

					@Override
					public void setName(String name) {

					}

					@Override
					public void setId(String _id) {

					}

					@Override
					public void isOr(boolean bool) {
						if (bool) {
							XGPushManager.unregisterPush(SetUserActivity.this);
							SystemApplication.getInstance().stopSoket();
							Intent intent = new Intent(SetUserActivity.this, Login.class);
							intent.putExtra("back", true);
							intent.putExtra("gone", true);
							startActivity(intent);
							SharedPreferencesDB.getInstance(SetUserActivity.this).setIsLogin(false);
							SharedPreferencesDB.getInstance(SetUserActivity.this).clear();
							finish();
						}
					}
				});
				sureCanclePop.show();

			}
		});
		slipswitch_MSL.setOnSwitchListener(new OnSwitchListener() {

			@Override
			public void onSwitched(boolean isSwitchOn) {
				// TODO Auto-generated method stub
				if (isSwitchOn) {
					SureCanclePop sureCanclePop = new SureCanclePop(SetUserActivity.this, "切换此状态后您将不再接受指定提问，但“未回复”和“进行中”问题仍需继续解答");
					sureCanclePop.setWindflowerDelegate(new WindflowerDelegate() {

						@Override
						public void setName(String name) {

						}

						@Override
						public void setId(String _id) {

						}

						@Override
						public void isOr(boolean bool) {
							if (bool) {
								postStopService("true");

							} else {
								SharedPreferencesDB.getInstance(SetUserActivity.this).setAcceptQuestion(false);
								slipswitch_MSL.setSwitchState(false);
							}
						}
					});
					sureCanclePop.show();
				} else {
					postStopService("false");
				}

			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		tv_password = (RelativeLayout) findViewById(R.id.set_password);
		tv_title = (TextView) findViewById(R.id.textView);
		iv_backs = (ImageView) findViewById(R.id.back_btn);
		tv_sure = (RelativeLayout) findViewById(R.id.tv_sure);
		iv_user = (RoundedImageView) findViewById(R.id.iv_user);
		tv_phone = (TextView) findViewById(R.id.tv_no);
		tv_email = (TextView) findViewById(R.id.tv_email);
		tv_finish = (TextView) findViewById(R.id.finish);
		tv_title.setText("设置");
		name=SharedPreferencesDB.getInstance(this).getAccountName();
		tv_phone.setText(name);
		imgurl = getIntent().getStringExtra("imgurl");
		stopService = getIntent().getBooleanExtra("stopService", false);
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.loading_img).showImageOnFail(R.drawable.list_default_icon)
				.showImageForEmptyUri(R.drawable.list_default_icon).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_8888).build();
		SystemApplication.getInstance().mImageLoad1.displayImage(imgurl, iv_user, options);
		slipswitch_MSL = (MySlipSwitch) findViewById(R.id.main_myslipswitch);
		if (stopService) {
			slipswitch_MSL.setSwitchState(true);
		} else {
			slipswitch_MSL.setSwitchState(false);
		}

	}

	/*
	 * 请求医生是否接受问题
	 */
	private void postStopService(String isStop) {
		String url = Constent.URL + "doctor/stopService";
		HttpMethod method = HttpMethod.POST;
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(new JsonParser(), QuestionAction.class) {

			@Override
			protected void handleResult(HttpResponseInfo<QuestionAction> responseInfo) {

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub

			}
		};
		HttpRequestParams params = new HttpRequestParams(method, url, requestCallBack);
		params.addBodyParameter("doctorId", SharedPreferencesDB.getInstance(this).getDocotorId());
		params.addBodyParameter("status", isStop);
		if (isStop.equals("true")) {
			SharedPreferencesDB.getInstance(SetUserActivity.this).setAcceptQuestion(false);
		} else {
			SharedPreferencesDB.getInstance(SetUserActivity.this).setAcceptQuestion(true);
		}
		HttpRequest.send(params);
	}
/*
 * 退出应用向服务器发送消息
 */
	private void postTime() {
		String url = Constent.URL+"doctor/logout";
		HttpMethod method = HttpMethod.POST;
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(
				new JsonParser(), QuestionAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<QuestionAction> responseInfo) {
				if (responseInfo.success != null) {

				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub

			}
		};
		finish();
		Intent intent = new Intent(SetUserActivity.this, Login.class);
		intent.putExtra("back", true);
		intent.putExtra("souce", true);
		intent.putExtra("gone", true);
		startActivity(intent);
		SharedPreferencesDB.getInstance(this).setIsLogin(false);
		SharedPreferencesDB.getInstance(this).clear();
		SharedPreferencesDB.getInstance(this).setAccountName(name);
		HttpRequestParams params = new HttpRequestParams(method, url,
				requestCallBack);
		params.addBodyParameter("doctorId",
				SharedPreferencesDB.getInstance(this).getDocotorId());
		HttpRequest.send(params);
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_set;
	}

}
